package dao;

import java.util.Date;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import static org.hibernate.criterion.Example.create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for Test
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see dao.Test
 * @author MyEclipse Persistence Tools
 */

public class TestDAO extends BaseHibernateDAO {
    private static final Logger log = LoggerFactory.getLogger(TestDAO.class);
    // property constants
    public static final String USERID = "userid";
    public static final String COURSEID = "courseid";
    public static final String SCORE = "score";
    public static final String UNABLEKNOWLEDGES = "unableknowledges";

    public boolean save(Test transientInstance) {
	return save(log, "saving Test instance", transientInstance);
    }

    public boolean delete(Test persistentInstance) {
	return delete(log, "deleting Test instance", persistentInstance);
    }

    public Test findById(java.lang.Integer id) {
	log.debug("getting Test instance with id: " + id);
	try {
	    Test instance = (Test) getSession().get("dao.Test", id);
	    return instance;
	} catch (RuntimeException re) {
	    log.error("get failed", re);
	    throw re;
	}
    }

    public List<Test> findByExample(Test instance) {
	log.debug("finding Test instance by example");
	try {
	    List<Test> results = (List<Test>) getSession()
		    .createCriteria("dao.Test").add(create(instance)).list();
	    log.debug("find by example successful, result size: "
		    + results.size());
	    return results;
	} catch (RuntimeException re) {
	    log.error("find by example failed", re);
	    throw re;
	}
    }

    public List findByProperty(String propertyName, Object value) {
	log.debug("finding Test instance with property: " + propertyName
		+ ", value: " + value);
	try {
	    String queryString = "from Test as model where model."
		    + propertyName + "= ?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setParameter(0, value);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find by property name failed", re);
	    throw re;
	}
    }

    public List<Test> findByUserid(Object userid) {
	return findByProperty(USERID, userid);
    }

    public List<Test> findByCourseid(Object courseid) {
	return findByProperty(COURSEID, courseid);
    }

    public List<Test> findByScore(Object score) {
	return findByProperty(SCORE, score);
    }

    public List<Test> findByUnableknowledges(Object unableknowledges) {
	return findByProperty(UNABLEKNOWLEDGES, unableknowledges);
    }

    public List findAll() {
	log.debug("finding all Test instances");
	try {
	    String queryString = "from Test";
	    Query queryObject = getSession().createQuery(queryString);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find all failed", re);
	    throw re;
	}
    }

    public Test merge(Test detachedInstance) {
	log.debug("merging Test instance");
	try {
	    Test result = (Test) getSession().merge(detachedInstance);
	    log.debug("merge successful");
	    return result;
	} catch (RuntimeException re) {
	    log.error("merge failed", re);
	    throw re;
	}
    }

    public boolean update(Test instance) {
	return update(log, "updating Test instance", instance);
    }

    public void attachClean(Test instance) {
	log.debug("attaching clean Test instance");
	try {
	    getSession().lock(instance, LockMode.NONE);
	    log.debug("attach successful");
	} catch (RuntimeException re) {
	    log.error("attach failed", re);
	    throw re;
	}
    }
}